package com.rikai.Rikai;


import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;

public class SplashLayout extends TextView implements Runnable {
  Paint paint;
  private Handler handler;
  private Random r;
  private class Sparkle {
     char c;
     int color;
     double x;
     double y;
     double scale;
     double dx;
     double dy;
     double dscale;
     double ddx;
     double ddy;
     double alpha;
  };
  private LinkedList<Sparkle> sparkles;
  int sparkleCount;

  private void init() {
    paint = new Paint();
    paint.setAntiAlias(false);
    r = new Random();
    sparkles = new LinkedList<Sparkle>();
    handler = new Handler();
  }
	public SplashLayout(Context context) {
		super(context);
    init();
	}
	public SplashLayout(Context context, AttributeSet attributeSet) {
		super(context,attributeSet);
    init();
	}
	public SplashLayout(Context context, AttributeSet attributeSet, int defStyle) {
		super(context,attributeSet, defStyle);
    init();
	}
  public void addSparkle(double x, double y, int count) {
    if (sparkleCount == 0) {
      handler.postDelayed(this, 40);
    }
    final String chars = "*\u2665\u2666";
    for (int i=0; i<count; ++i) {
      Sparkle s = new Sparkle();
      s.c = chars.charAt(r.nextInt(chars.length()));
      switch(r.nextInt(4)) {
        case 0: s.color = 0xFFFFFFFF; break;
        case 1: s.color = 0xFFFF0000; break;
        case 2: s.color = 0xFF00FF00; break;
        case 3: s.color = 0xFF0000FF; break; 
      }
      s.alpha = 255;
      double angle = r.nextDouble() * Math.PI * 2;
      double velocity = r.nextDouble() * 4 + 4.0;
      s.dx = velocity * Math.sin(angle);
      s.dy = velocity * Math.cos(angle);
      s.x = x + s.dx*3;
      s.y = y + s.dy*3;
      s.scale = r.nextDouble() * 8 + 16;
      s.dscale = -r.nextDouble()*2-2;
      sparkles.add(s);
      sparkleCount+=1;
    }
    while (sparkleCount > 10) {
      sparkles.remove(0);
      sparkleCount -= 1;
    }
  }
  public void addSparkle(double x, double y) {
    addSparkle(x,y);
  }
  @Override protected void onDraw(Canvas canvas) {
    long nanos = System.nanoTime();
    //super.onDraw(canvas);
    ListIterator<Sparkle> i = sparkles.listIterator(sparkleCount);
    char[] txt = new char[1];
    while (i.hasPrevious() && System.nanoTime() - nanos < 1000000) {
      Sparkle s = i.previous();
      txt[0] = s.c;
      paint.setColor(s.color);
      paint.setTextSize((float)s.scale);
      canvas.drawText(txt, 0, 1, (float)s.x, (float)s.y, paint);
    }
  }

  public void run() {
    ListIterator<Sparkle> i = sparkles.listIterator();
    while (i.hasNext()) {
      Sparkle s = i.next();
      s.dx += s.ddx;
      s.dy += s.ddy;
      s.x += s.dx;
      s.y += s.dy;
      s.scale += s.dscale;
      s.alpha -= 8.0;
      if (s.alpha<=64 || s.scale < 1) {
    	  i.remove();
    	  sparkleCount -=1;
      } else {
    	  s.color = s.color & 0x00FFFFFF + (((int)s.alpha) << 8*3);
        s.color = s.color | 0xFF000000;
      }
    }
    postInvalidate();
    if (sparkleCount>0) {
      handler.postDelayed(this, 40);
    }
  }
}
